const SortingAlgorithms = require("../template")

function testSpecialSorts() {
  const sorter = new SortingAlgorithms()

  console.log(
    "1. 计数排序基础测试:",
    (() => {
      const countingArray = [4, 2, 2, 8, 3, 3, 1]
      const expectedCounting = [1, 2, 2, 3, 3, 4, 8]
      const result = sorter.countingSort([...countingArray])
      return JSON.stringify(result) === JSON.stringify(expectedCounting)
    })()
  )

  console.log(
    "2. 计数排序边界情况测试:",
    (() => {
      return JSON.stringify(sorter.countingSort([])) === JSON.stringify([]) &&
             JSON.stringify(sorter.countingSort([5])) === JSON.stringify([5]) &&
             JSON.stringify(sorter.countingSort([3, 3, 3, 3])) === JSON.stringify([3, 3, 3, 3])
    })()
  )

  console.log(
    "3. 基数排序基础测试:",
    (() => {
      const radixArray = [170, 45, 75, 90, 2, 802, 24, 66]
      const expectedRadix = [2, 24, 45, 66, 75, 90, 170, 802]
      const result = sorter.radixSort([...radixArray])
      return JSON.stringify(result) === JSON.stringify(expectedRadix)
    })()
  )

  console.log(
    "4. 基数排序边界情况测试:",
    (() => {
      return JSON.stringify(sorter.radixSort([])) === JSON.stringify([]) &&
             JSON.stringify(sorter.radixSort([42])) === JSON.stringify([42]) &&
             JSON.stringify(sorter.radixSort([111, 111, 111])) === JSON.stringify([111, 111, 111])
    })()
  )

  console.log(
    "5. 大范围数字基数排序测试:",
    (() => {
      const largeRangeArray = [1000, 1, 999, 2, 998, 3, 997, 4, 996, 5]
      const expectedLargeRange = [1, 2, 3, 4, 5, 996, 997, 998, 999, 1000]
      const result = sorter.radixSort([...largeRangeArray])
      return JSON.stringify(result) === JSON.stringify(expectedLargeRange)
    })()
  )

  console.log(
    "6. 零值基数排序测试:",
    (() => {
      const zeroArray = [0, 100, 0, 10, 0, 1]
      const expectedZero = [0, 0, 0, 1, 10, 100]
      const result = sorter.radixSort([...zeroArray])
      return JSON.stringify(result) === JSON.stringify(expectedZero)
    })()
  )

  console.log(
    "7. 特殊排序算法一致性测试:",
    (() => {
      const testArray = [4, 2, 2, 8, 3, 3, 1]
      const countingResult = sorter.countingSort([...testArray])
      const radixResult = sorter.radixSort([...testArray])
      return JSON.stringify(countingResult) === JSON.stringify(radixResult)
    })()
  )

  console.log(
    "8. 特殊排序与通用排序一致性测试:",
    (() => {
      const testArray = [4, 2, 2, 8, 3, 3, 1]
      const countingResult = sorter.countingSort([...testArray])
      const bubbleResult = sorter.bubbleSort([...testArray])
      return JSON.stringify(countingResult) === JSON.stringify(bubbleResult)
    })()
  )

  console.log(
    "9. this上下文特殊排序测试:",
    (() => {
      const specialObj = {
        multiplier: 2,
        processSpecialSort: function(arr) {
          const sorter = new SortingAlgorithms()
          const result = sorter.countingSort([...arr])
          return result.map(item => item * this.multiplier)
        }
      }
      const result = specialObj.processSpecialSort([3, 1, 2])
      return JSON.stringify(result) === JSON.stringify([2, 4, 6])
    })()
  )

  console.log(
    "10. 复杂对象this绑定特殊排序测试:",
    (() => {
      const specialConfigObj = {
        config: { offset: 5 },
        processWithConfig: function(arr) {
          const sorter = new SortingAlgorithms()
          const result = sorter.radixSort([...arr])
          return result.map(item => item + this.config.offset)
        }
      }
      const result = specialConfigObj.processWithConfig([2, 1, 3])
      return JSON.stringify(result) === JSON.stringify([6, 7, 8])
    })()
  )
}

testSpecialSorts()
